package com.one.intercept;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author ShiWen
 * @date 2022/1/20 8:38
 * @Version 1.0
 * @Description TODO
 */
@Component
public class FeignBeforeExecInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //1.RequestContextHolder拿到当前请求的数据，相当与拿到controller入参的HttpServletRequest
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (requestAttributes!=null){
            //老请求
            HttpServletRequest request = requestAttributes.getRequest();

            if (request!=null){
                //2.同步请求头信息->cookie
                String cookie = request.getHeader("Cookie");
                template.header("Cookie",cookie).header("token",request.getHeader("token"));
            }
        }
//        String cookie = request.getHeader("Cookie");
//            template.header("Cookie",cookie);

    }
}
